<?php

namespace App\Http\Controllers;

use App\Models\Ad;
use App\Models\Article;
use App\Models\Category;
use App\Models\Journal;
use App\Models\OrderComment;
use App\Models\UsersLeaveMsg;
use App\Models\Video;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Ll\DcatConfig\Models\LlConfig;

class HomeController extends BaseController
{

    /**
     * 广告
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-02
     * @return void
     */
    public function getAds()
    {
        return success(Ad::where('status', 1)->select('id', 'title', 'img', 'type', 'is_jump', 'href')->orderBy('id', 'desc')->get());
    }

    /**
     * 获取配置
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-16
     * @return void
     */
    public function getConfig()
    {
        return success(LlConfig::all(['name', 'value', 'value as value_text', 'option', 'description']));
    }

    /**
     * 在线留言
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function leaveMsg(Request $request)
    {

        $validator = Validator::make($request->input(), [
            'username' => 'required',
            'phone' => 'required|mobile',
            'email' => 'required|email',
            'content' => 'required',
        ], [
            'username.required' => '姓名不能为空',
            'phone.required' => '手机号不能为空',
            'phone.mobile' => '手机号不正确',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱不正确',
            'content.required' => '留言内容不能为空',
        ]);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $insert = $request->input();
        $msgModel = new UsersLeaveMsg();
        $msgModel->username = $insert['username'];
        $msgModel->phone = $insert['phone'];
        $msgModel->email = $insert['email'];
        $msgModel->content = $insert['content'];
        $msgModel->ip = $request->ip();
        $msgModel->save();

        return success();
    }


    /**
     * 客户反馈
     *
     * @return string
     */
    public function feedback(Request $request)
    {
        $rules = [
            'page' => ['required'],
            'limit' => ['required'],
        ];

        $message = [
            'page.required' => '参数错误-页码',
            'limit.required' => '参数错误-条数',
        ];

        $payload = $request->only('page', 'limit');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $list = OrderComment::where(['status' => 1])
            ->orderBy('id', 'desc')
            ->paginate($payload['limit']);

        return successPaginate($list);
    }


    /**
     * 精选文章
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-02-27
     * @return void
     */
    public function featuredArticles()
    {
        $articles = new Article();
        $result = $articles->getRecommendArticle();
        return success($result);
    }

    /**
     * 精选视频
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-02-27
     * @return void
     */
    public function featuredVideo()
    {
        $video = new Video();
        $result = $video->getRecommendVideo();
        return success($result);
    }

    /**
     * 信息资源
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-02-28
     * @param Request $request
     * @return void
     */
    public function informationResources(Request $request)
    {
        $rules = [
            'cate_id' => ['required'],
            // 'title' => ['required'],
            'page' => ['required'],
            'limit' => ['required'],

        ];

        $message = [
            'cate_id.required' => '请选择分类',
            // 'title.required' => '密码不能为空',
            'page.required' => '参数错误-页码',
            'limit.required' => '参数错误-条数',
        ];

        $payload = $request->only('cate_id', 'title', 'page', 'limit');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        if ($payload['cate_id'] == 1) {
            // 精选文章
            $articles = new Article();
            $list = $articles->getArticleList($payload);
        } elseif ($payload['cate_id'] == 2) {
            // 精选视频
            $video = new Video();
            $list = $video->getVideoList($payload);
        }
        return successPaginate($list);
    }

    /**
     * 信息资源详情
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-10
     * @param Request $request
     * @return void
     */
    public function informationResourcesDetail(Request $request)
    {
        $rules = [
            'cate' => ['required'],
            'id' => ['required'],
        ];

        $message = [
            'cate.required' => '请选择分类',
            'id.required' => '参数错误-id',
        ];

        $payload = $request->only('cate', 'id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        if ($payload['cate'] == 'article') {
            // 精选文章详情
            $articles = new Article();
            $detail = $articles->getArticleDetail($payload['id']);
        } elseif ($payload['cate'] == 'video') {
            // 精选视频详情
            $video = new Video();
            $detail = $video->getVideoDetail($payload['id']);
        }
        return success($detail);
    }


    /**
     * 首页-主力作者发表影响力文章（订单评价）
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-04-25
     * @param Request $request
     * @return void
     */
    public function comment(Request $request)
    {
        return success(OrderComment::where('status', 1)->select('id', 'title', 'title_href', 'cover_page', 'organization', 'content', 'username', 'journal_name', 'journal_area', 'created_at')->orderBy('created_at', 'desc')->limit(3)->get());
    }

    /**
     * 期刊列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-08
     * @param Request $request
     * @return void
     */
    public function journalList(Request $request)
    {
        $rules = [
            'page' => ['required'],
            'limit' => ['required'],

        ];

        $message = [
            'page.required' => '参数错误-页码',
            'limit.required' => '参数错误-条数',
        ];

        $payload = $request->only('page', 'limit', 'name', 'issn_eissn', 'type', 'open_access', 'jif_2022_min', 'jif_2022_max');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $list = Journal::getJournalList($payload);
        return successPaginate($list);
    }

    /**
     * 期刊详情
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-09
     * @param Request $request
     * @return void
     */
    public function journalDetail(Request $request)
    {
        $rules = [
            'id' => ['required'],


        ];

        $message = [
            'id.required' => '参数错误id',
        ];

        $payload = $request->only('id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        return success(Journal::getJournalDetail($payload['id']));
    }

    /**
     * 期刊分类
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-09
     * @param Request $request
     * @return void
     */
    public function journalCate(Request $request)
    {
        return success(Category::where('status', 1)->select('id', 'title')->get());
    }
}
